//
//  StatusOriginalView.swift
//  sinaWeibo
//
//  Created by liii on 16/7/24.
//  Copyright © 2016年 liii. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let StatusCellMargin:CGFloat = 10

class StatusOriginalView: UIView {
    
    // 保存约束
    var bottomConstraint: Constraint?
    
    var status: Status? {
        didSet {
            // 绑定模型数据
            iconView.sd_setImageWithURL(status?.user?.headImageURL)
            vertified_image.image = status?.user?.verified_type_image
            nameLabel.text = status?.user?.name
            mbrank.image = status?.user?.mbrank_image
            timeLabel.text = status?.created_time
            sourceLable.text = status?.sourceText
            contenLabel.attributedText = EmoticonManager.sharedEmotionManager.replaceTextToImage(status?.text ?? "", font: contenLabel.font)
            
            // 卸载约束
            bottomConstraint?.uninstall()
            
            // 判断原创微博是否有配图
            if let urls = status?.imageURLs where urls.count != 0 {
                // 有配图
                pictureView.hidden = false
                pictureView.imageURLs = urls
                
                // 更新self的bottom约束, 相对pictureView的bottom
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                // 没有配图
                pictureView.hidden = true
                pictureView.imageURLs = nil
                
                // 更新self的bottom约束, 相对contentLabel的bottom
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contenLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 构建UI界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        
        // 设置Cell间的分隔线的背景色
        sepView.backgroundColor = UIColor.grayColor()
        
        // 添加子控件
        addSubview(sepView)
        addSubview(iconView)
        addSubview(vertified_image)
        addSubview(nameLabel)
        addSubview(mbrank)
        addSubview(timeLabel)
        addSubview(sourceLable)
        addSubview(contenLabel)
        addSubview(pictureView)
        
        // 分隔线
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        // 约束子控件
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.width.equalTo(36)
            make.height.equalTo(36)
        }
        vertified_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        mbrank.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contenLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contenLabel.snp_left)
            make.top.equalTo(contenLabel.snp_bottom).offset(StatusCellMargin)
            // 测试宽高
            // make.size.equalTo(CGSizeMake(100, 100))
        }
        
        // 头像圆角处理
        iconView.layer.cornerRadius = 18
        iconView.layer.masksToBounds = true
        
        // 原创微博的高度计算
        self.snp_makeConstraints { (make) -> Void in
            // 1.设置约束; 2.保存约束
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    // 顶部分割视图
    private lazy var sepView: UIView = UIView()
    
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 会员图标
    private lazy var mbrank: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 认证类型
    private lazy var vertified_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    // 昵称
    private lazy var nameLabel: UILabel = UIFactory.labelWithText("新浪微博测试", fontSize: 15, textColor: UIColor.darkGrayColor())
    // 发布时间
    private lazy var timeLabel: UILabel = UIFactory.labelWithText("2016-7-24", fontSize: 11, textColor: UIColor.darkGrayColor())
    // 微博来源
    private lazy var sourceLable: UILabel = UIFactory.labelWithText("测试来源", fontSize: 11, textColor: UIColor.darkGrayColor())
    // 微博正文
    private lazy var contenLabel: UILabel = UIFactory.labelWithText("测试数据", fontSize: 15, textColor: UIColor.darkGrayColor(), alignment: .Left)
    // 微博配图
    private lazy var pictureView: StatusOriginalViewPictureView = StatusOriginalViewPictureView()
    
}
